package com.hongdee.crm.task.dao.impl;

import com.hongdee.base.basedao.impl.BaseDaoImpl;
import com.hongdee.crm.maintain.entity.TCrmCarMaintain;
import com.hongdee.crm.task.dao.ITCrmCarMaintainMileTaskDao;
import com.hongdee.crm.task.dao.ITCrmCarMaintainTaskDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created by My on 2017/3/6.
 */
@Service
public class TCrmCarMaintainMileTaskDaoImpl extends BaseDaoImpl implements ITCrmCarMaintainMileTaskDao {

    @Autowired
    @Qualifier("jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Override
    public Map<String, Object> findLastMainTailMileInfo(String type,String companyId) throws Exception {
        Map<String,Object> map=this.getJdbcDao().queryForMapBySqlWithParams(
                "select * from t_crm_car_maintain where maintian_type=? and company_id=? order by maintain_time DESC LIMIT 0,1",type,companyId);
        if(map.size()>0){
            return map;
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> findMainTainList(String sqlStr,List<Object> params) throws Exception {
        List<Map<String,Object>> mainTainList=null;
//        if(lastTime!=null){
//            mainTainList=this.getJdbcDao().queryForMapListBySqlWithParams(sqlStr,companyId,type,lastTime,start,limit);
//        }else{
        mainTainList=this.getJdbcDao().queryForMapListBySqlWithParams(sqlStr,params.toArray());
//        }
        if(mainTainList.size()>0){
            return mainTainList;
        }
        return null;
    }

    @Override
    public Map<String, Object> findLastMainTainInfo(String companyId, String carId,String type) throws Exception {
        String sqlStr="select * from t_crm_car_maintain where company_id=? and car_id=? and maintian_type=? order by maintain_time desc limit 0,1";
        Map<String,Object> map=this.getJdbcDao().queryForMapBySqlWithParams(sqlStr,companyId,carId,type);
        if(map.size()>0){
            return map;
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> findDbMainTainList(String companyId,Long maintainTime, String carId) throws Exception {
        String sqlStr="select * from (select * from t_crm_car_maintain where car_id=? " +
                "and company_id=? and maintain_time<=? and avg_maintain_mile is not null order by maintain_time DESC limit 0,3) t order by t.maintain_time ASC";
        List<Map<String,Object>> lists=this.getJdbcDao().queryForMapListBySqlWithParams(sqlStr,carId,companyId,maintainTime);
        if(lists.size()>0){
            return lists;
        }
        return null;
    }

    @Override
    public Integer findWorderCounts(String worderId) throws Exception {
        String sqlStr="select * from t_crm_car_maintain where worder_id=?";
        List<Map<String,Object>> lists=this.getJdbcDao().queryForMapListBySqlWithParams(sqlStr,worderId);
        return lists.size();
    }

    /**
     * 批量插入车辆保养信息
     * @param lists
     */
    @Override
    public void insertMaintailLists(final List<TCrmCarMaintain> lists){
        String sql="insert into" +
                "        t_crm_car_maintain" +
                "        (avg_maintain_mile, car_id, company_id, " +
                "contact_name, contact_phone, maintain_mile, maintain_time, maintian_type, next_maintain_time, plate_num, vin, worder_id,next_maintain_mile, id) " +
                "    values" +
                "        ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, replace(UUID(),'-',''))";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                TCrmCarMaintain crmCarMaintain=lists.get(i);
                ps.setString(1,crmCarMaintain.getAvgMaintainMile());
                ps.setString(2,crmCarMaintain.getCarId());
                ps.setString(3,crmCarMaintain.getCompanyId());
                ps.setString(4,crmCarMaintain.getContactName());
                ps.setString(5,crmCarMaintain.getContactPhone());
                ps.setString(6,crmCarMaintain.getMaintainMile());
                ps.setLong(7,crmCarMaintain.getMaintainTime());
                ps.setString(8,crmCarMaintain.getMaintainType());
                ps.setLong(9,crmCarMaintain.getNextMaintainTime());
                ps.setString(10,crmCarMaintain.getPlateNum());
                ps.setString(11,crmCarMaintain.getVin());
                ps.setString(12,crmCarMaintain.getWorderId());
                ps.setString(13,crmCarMaintain.getNextMaintainMile());
            }

            @Override
            public int getBatchSize() {
                return lists.size();
            }
        });
    }

    @Override
    public void insertAndDelMaintailLists(List<String> lists){
        String[] sqls=lists.toArray(new String[lists.size()]);
        jdbcTemplate.batchUpdate(sqls);
    }

    @Override
    public void updCarNextMaintainLists(final List<TCrmCarMaintain> lists){
        String sql="update t_crm_car set next_maintain_date=? ,next_maintain_mile=? where id=?";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                TCrmCarMaintain crmCarMaintain=lists.get(i);
                //本次保养的里程数
                String runMile=crmCarMaintain.getMaintainMile();
                //上次保养时间
                Long maintainTime=crmCarMaintain.getMaintainTime();
                //平均保养里程
                String avgMile=crmCarMaintain.getAvgMaintainMile();
                Float days=(crmCarMaintain.getNextMaintainTime()-maintainTime)/(60*60*24*1000F);
                Float addMile=days*Float.valueOf(avgMile);
                Float sumMile=Float.valueOf(runMile)+addMile;
                ps.setString(1,crmCarMaintain.getNextMaintainTime().toString());
                ps.setString(2,String.valueOf(sumMile.intValue()));
                ps.setString(3,crmCarMaintain.getCarId());

            }

            @Override
            public int getBatchSize() {
                return lists.size();
            }
        });
    }



}
